package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.VO.PageInfoResult;
import com.student.dto.ResponseDto;
import com.student.mapper.ArtistSchedulesMapper;
import com.student.pojo.ArtistSchedules;
import com.student.pojo.Artists;
import com.student.pojo.ClickRecords;
import com.student.service.ArtistSchedulesService;
import com.student.util.PageResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: SupportGroup
 * @BelongsPackage: com.student.service.impl
 * @Author: 小徐
 * @CreateTime: 2025-02-28 15:44
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class ArtistSchedulesServiceImpl implements ArtistSchedulesService {
    @Autowired
    private ArtistSchedulesMapper artistSchedulesMapper;

    @Override
    public ResponseDto getAllArtistSchedules(ArtistSchedules artistSchedules) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(artistSchedules.getCurrentPage(), artistSchedules.getPageSize());
        List<ArtistSchedules> snackGoodsCates = artistSchedulesMapper.artistsMessage(artistSchedules);

        PageInfo<ArtistSchedules> cyCardPageInfo = new PageInfo<>(snackGoodsCates);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto addArtistSchedules(ArtistSchedules artistSchedules) {
        //切割时间这边设置开始时间
        artistSchedules.setStartTime(artistSchedules.getEndTime().split("-")[0]);
//        切割时间这边设置结束时间
        artistSchedules.setEndTime(artistSchedules.getEndTime().split("-")[1]);
        Integer res = artistSchedulesMapper.addArtistSchedules(artistSchedules);
        if (res > 0) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto updateArtistSchedules(ArtistSchedules artistSchedules) {
        Integer res = artistSchedulesMapper.updateArtistSchedules(artistSchedules);
        if (res > 0) {
            return ResponseDto.success(res);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto getAllArtistSchedulesPerson(ArtistSchedules artistSchedules) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(artistSchedules.getCurrentPage(), artistSchedules.getPageSize());
        List<ArtistSchedules> snackGoodsCates = artistSchedulesMapper.getAllArtistSchedulesPerson(artistSchedules);

        PageInfo<ArtistSchedules> cyCardPageInfo = new PageInfo<>(snackGoodsCates);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto SingleArtistSchedules(Integer id) {
        ArtistSchedules artistSchedules = artistSchedulesMapper.SingleArtistSchedules(id);
        if (artistSchedules != null) {
            return ResponseDto.success(artistSchedules);
        }

        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto toggleLike(Integer id, Integer likesCount, Integer artistId) {
        List<ClickRecords> clickRecords = artistSchedulesMapper.select(id, artistId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        boolean isTodayLiked = false; // 用于标记今天是否已经点赞过
        ClickRecords todayClickRecord = null; // 用于存储今天的点赞记录

// 遍历点赞记录，查找今天的记录
        for (ClickRecords record : clickRecords) {
            try {
                Date targetDate = sdf.parse(record.getClickDate());
                Date currentDate = new Date();

                String formattedTargetDate = sdf.format(targetDate);
                String formattedCurrentDate = sdf.format(currentDate);

                if (formattedCurrentDate.equals(formattedTargetDate)) {
                    isTodayLiked = true;
                    todayClickRecord = record;
                    break; // 找到今天的记录后退出循环
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (isTodayLiked) {
            // 如果今天已经有点赞记录
            if (todayClickRecord.getClickCount() >= 5) {
                // 如果今天已经点赞五次，返回错误信息
                return new ResponseDto(2, "每天只能点赞五次", null);
            } else {
                // 如果今天点赞次数未达到五次，更新点赞次数和点赞数量
                artistSchedulesMapper.update(todayClickRecord.getId(), todayClickRecord.getClickCount() + 1);
                artistSchedulesMapper.toggleLike(id, likesCount, artistId);
                return new ResponseDto(1, "点赞成功", null);
            }
        } else {
            // 如果今天没有点赞记录，创建新的点赞记录，并更新点赞数量
            ClickRecords newClickRecord = new ClickRecords();
            newClickRecord.setUserId(id); // 设置用户 ID
            newClickRecord.setArtistId(artistId); // 设置艺人 ID
            newClickRecord.setClickDate(sdf.format(new Date())); // 设置当前日期
            newClickRecord.setClickCount(1); // 初始化点赞次数为 1
            artistSchedulesMapper.insert(newClickRecord);
            artistSchedulesMapper.toggleLike(id, likesCount + 1, artistId);
            return new ResponseDto(1, "点赞成功", null);
        }

    }


}